// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-07 16:26
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.TREE;

import com.DataStruct.Tree.TwoBinaryTree.TreeNode;
import com.DataStruct.Tree.TwoBinaryTree.Utils;
import org.junit.jupiter.api.Test;

@SuppressWarnings("ALL")
public class SortedArrayToBST_ {
    /**
     * 将升序序列包装成二叉排序树
     */
    public TreeNode sortedArrayToBST(int[] nums) {
        // 递归创建 -> 创建的时候使用中间位置的数据作为根节点 -> 使得左右子树的根节点数量一样
        // 以达到平衡二叉树的状态
        if (nums.length == 0) {
            return null;
        }
        return constructTreeBySortedArray(nums, 0, nums.length - 1);

    }

    private TreeNode constructTreeBySortedArray(int[] nums, int start, int end) {
        if (start > end) {
            return null;
        }
        int mid = (start + end) >> 1;
        TreeNode node = new TreeNode(nums[mid]);
        node.right = constructTreeBySortedArray(nums, mid + 1, end);
        node.left = constructTreeBySortedArray(nums, start, mid - 1);
        return node;
    }

    @Test
    public void shout() {
        int[] sortedArray = {-10, -3, 0, 5, 9};
        Utils utils = new Utils();
        TreeNode root = sortedArrayToBST(sortedArray);
        System.out.println(utils.sequenceSearch(root));
    }
}
